<?php
defined('InNcPHP') or exit('Access Invalid!');

/**
 * 广告轮播图
 * Class advertControl
 */
class advertControl extends SystemControl{

    private $links = array(
        array('url'=>'act=advert&op=index','lang'=>'advert_index'),
        array('url'=>'act=advert&op=advert_add','lang'=>'nc_new')
    );

    public function __construct(){
        parent::__construct();
        Language::read('advert');
    }

    /**
     * 广告列表
     */
    public function indexOp(){
        /* @var advertModel $model */
        $model = Model('advert');
        if (chksubmit()){
            if (!empty($_POST['del_id'])){
                if (is_array($_POST['del_id'])){
                    foreach ($_POST['del_id'] as $k => $v){
                        $model->delAdvert(array('advert_id'=>intval($v)));
                    }
                }
                $this->log(L('nc_delete,advert_index_advert_name').'[advert_id:'.intval($_POST['del_id']).']',1);
                showMessage(L('nc_common_del_succ'));
            }else {
                $this->log(L('nc_delete,advert_index_advert_name').'[advert_id:'.intval($_POST['del_id']).']',0);
                showMessage(L('nc_common_del_succ'));
            }
        }
        $list = $model->getAdvertListPage(array(),'*',10,'advert_type asc,advert_sort asc,advert_id asc');
        Tpl::output('list',$list);
        Tpl::output('advert_type_list',$model::$advert_type);
        Tpl::output('top_link', $this->sublink($this->links, 'index'));
        Tpl::output('page',$model->showpage());
        Tpl::showpage('advert.index');
    }


    // 删除
    function advert_delOp(){
        if (!empty($_GET['advert_id'])){
            $model = Model('advert');
            $model->delAdvert(array('advert_id'=>intval($_GET['advert_id'])));
            $this->log(L('nc_delete,advert_index_advert_name').'[advert_id:'.intval($_GET['advert_id']).']',1);
            showMessage(L('nc_common_del_succ'));
        }else {
            $this->log(L('nc_delete,advert_index_advert_name').'[advert_id:'.intval($_GET['advert_id']).']',0);
            showMessage(L('nc_common_del_fail'));
        }
    }

    //禁用\启用
    public function advert_updateOp(){
        if (!empty($_GET['advert_id']) && isset($_GET['state'])){
            $model = Model('advert');
            $model->updateAdvert(array('advert_id'=>intval($_GET['advert_id'])), array('state'=>$_GET['state']));
            $this->log(L('nc_edit,advert_index_advert_name').'[advert_id:'.intval($_GET['advert_id']).',state:'.$_GET['state'].']',1);
            showMessage(L('nc_common_op_succ'));
        }else {
            $this->log(L('nc_edit,advert_index_advert_name').'[advert_id:'.intval($_GET['advert_id']).',state:'.$_GET['state'].']',0);
            showMessage(L('nc_common_op_fail'));
        }
    }

    //新增
    function advert_addOp(){
        /* @var advertModel $model */
        $model = Model('advert');
        if(chksubmit()){
            $param['advert_name'] = $_POST['advert_name'];
            $param['advert_type'] = $_POST['advert_type'];
            $param['advert_url'] = $_POST['advert_url'];
            $param['advert_time'] = time();
            $param['advert_sort'] = $_POST['advert_sort'];

            $upload = new UploadFile();
            $upload->set('default_dir','product/'.$upload->getSysSetPath());
            $upload->set('max_size',2*1024);   //文件最大允许2M
            $result = $upload->upfile('advert_image');
            if($result){
                //得到图片上传后的路径
                $param['advert_image'] = $upload->getSysSetPath().$upload->file_name;
            }
            $rs = $model->addAdvert($param);
            if ($rs){
                $this->log(L('nc_add,advert_index_advert_name').'[advert_name:'.intval($_POST['advert_name']).']',1);
                showMessage(L('nc_common_save_succ'),'index.php?act=advert&op=index');
            }else {
                $this->log(L('nc_add,advert_index_advert_name').'[advert_name:'.intval($_POST['advert_name']).']',0);
                showMessage(L('nc_common_save_fail'));
            }
        }
        Tpl::output('advert_type_list',$model::$advert_type);
        Tpl::output('top_link', $this->sublink($this->links, 'advert_add'));
        Tpl::showpage('advert.add');
    }

    //编辑
    public function advert_editOp(){
        /* @var advertModel $model */
        $model = Model('advert');
        $advert_id = $_GET['advert_id'];
        if(chksubmit()){
            $advert_id = $_POST['advert_id'];
            $param['advert_name']       = $_POST['advert_name'];
            $param['advert_type'] = $_POST['advert_type'];
            $param['advert_url']       = $_POST['advert_url'];
            $param['advert_sort'] = $_POST['advert_sort'];
            if($_FILES['advert_image']['tmp_name'] != ''){
                $upload = new UploadFile();
                $upload->set('default_dir','product/'.$upload->getSysSetPath());
                $upload->set('max_size',2*1024);   //文件最大允许2M
                $result = $upload->upfile('advert_image');
                if($result){
                    //得到图片上传后的路径
                    $param['advert_image']    = $upload->getSysSetPath().$upload->file_name;
                }
            }
            $result = $model->updateAdvert(array('advert_id'=>$advert_id),$param);
            if($result){
                $this->log(L('nc_edit,advert_index_advert_name').'[advert_id:'.intval($_POST['advert_id']).']',1);
                showMessage(L('nc_common_save_succ'),'index.php?act=advert&op=index');
            }else {
                $this->log(L('nc_edit,advert_index_advert_name').'[advert_id:'.intval($_POST['advert_id']).']',0);
                showMessage(L('nc_common_save_fail'));
            }
        }
        $advertInfo = $model->getAdvertInfo(array('advert_id'=>$advert_id));
        Tpl::output('advertInfo',$advertInfo);
        Tpl::output('advert_type_list',$model::$advert_type);
        Tpl::output('top_link', $this->sublink($this->links, 'advert_edit'));
        Tpl::showpage('advert.edit');
    }
}

